﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;

using KPIReport.Library.DataAccess.Administration.UserManagement;
using KPIReport.Library.DomainObject.Administration.UserManagement;
//using KPIReport.Library.DomainObject.Administration.Catalog;
//using KPIReport.Library.DataAccess.Administration.Catalog;

using KPIReport.Web.Support;
//using KPIReport.Library.DataAccess.Administration.Catalog;
//using KPIReport.Library.DomainObject.Administration.Catalog;

namespace KPIReport.Web.Forms.UserManagement
{
    public partial class sUserform : BasePage
    {
        private int id = -1;
        private int status = -1;
        
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                hfStatus.Value = status.ToString();
                txtUserCode.Focus();
                if (Request.Params["Id"] != null)
                {
                    id = Convert.ToInt32(Request.Params["Id"]);
                }
                if (Request.Params["Status"] != null)
                {
                    status = Convert.ToInt32(Request.Params["Status"]);
                    hfStatus.Value = status.ToString();
                }

                //CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["KPIReportConn"].ConnectionString);
                //List<Cabang> list = CabangDal.GetList();
                //var query = list.OrderBy(x => x.NamaCabang);
                //list = query.ToList();
                //ddlCabang.Items.Clear();

                //list.ForEach(delegate(Cabang obj)
                //{
                //    ddlCabang.Items.Add(new ListItem(obj.NamaCabang, obj.CabangID.ToString()));
                //});

                //ddlCabang.Items.Insert(0, new ListItem("-- pilih cabang --", string.Empty));


                if (!Page.IsPostBack)
                {

                    if (id > 0) // Edit an existing item
                    {
                        txtUserName.Focus();
                        recUserRepo userDal = new recUserRepo(ConfigurationManager.ConnectionStrings["KPIReportConn"].ConnectionString);
                        recUser dto = userDal.GetById(id);

                        if (dto != null)
                        {

                            hfID.Value = id.ToString();
                            txtUserName.Text = dto.UserName;
                            txtUserCode.Text = dto.UserCode;
                            //txtEmail.Text = dto.Email;
                            //txtMobile.Text = dto.Mobile;
                            chkIsActive.Checked = dto.IsActive.HasValue ? dto.IsActive.Value : false;

                            trPassword.Visible = false;
                            trConfirmPassword.Visible = false;
                            txtUserCode.ReadOnly = true;
                            txtUserCode.Enabled = false;

                            //if(dto.CabangID!=null)
                            //    ddlCabang.Items.FindByValue(dto.CabangID.ToString()).Selected = true;
                        }
                    }

                    GetPrivilege();

                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "error", "alert('" + " Error :" + ex.Message + "');", true);
                return;
            }
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["KPIReportConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    
    }
}

